//
//  BuyerSeacherClassViewController.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/25.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "BuyerSeacherClassViewController.h"


@interface BuyerSeacherClassCell :UITableViewCell

@property (nonatomic,strong)UILabel * titlelab;

@property (nonatomic,strong)UIImageView * icon;


@end

@implementation BuyerSeacherClassCell



-(UILabel *)titlelab{        if (!_titlelab) {                UILabel * lab = [[UILabel alloc]init];        lab.textColor = rgbTitleDeepGray;        lab.font = Font(14);        lab.textAlignment = NSTextAlignmentLeft;        [self addSubview:lab];        _titlelab = lab;                    }        return _titlelab;}
-(UIImageView *)icon{        if (!_icon) {                UIImageView * imageview = [[UIImageView alloc]init];        imageview.clipsToBounds = YES;        [self addSubview:imageview];        _icon = imageview;    }        return _icon;}




@end

@interface BuyerSeacherClassViewController ()
@property(nonatomic,strong)UITableView * zhltableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,copy)void(^bolck)(NSDictionary * data,NSInteger index);
@end

@implementation BuyerSeacherClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.index = 0;
    
    self.dataArray = [[NSMutableArray alloc]init];
    
    self.bolck = self.otherInfo[@"block"];
    
    if ([self.userInfo isKindOfClass:[NSArray class]] || [self.userInfo isKindOfClass:[NSMutableArray class]]) {
    
        
        [self makeData];
        
    }
    
    // Do any additional setup after loading the view.
}
-(void)makeData{
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
    [dict setValue:@"所有" forKey:@"title"];
    [dict setValue:@"0" forKey:@"id"];
    
    [self.dataArray addObject:dict];
    
    
    for (int i = 0 ; i < [self.userInfo count]; i++) {
        
        NSDictionary * data = [self.userInfo objectAtIndex:i];
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setValue:[data valueForJSONStrKey:[self.otherInfo valueForJSONStrKey:@"title"]] forKey:@"title"];
        [dic setValue:[data valueForJSONStrKey:[self.otherInfo valueForJSONStrKey:@"id"]] forKey:@"id"];
        [self.dataArray addObject:dic];
    
    }
    
    self.index = [[self.otherInfo valueForJSONStrKey:@"index"] integerValue];
    
    
    [self.zhltableView reloadData];
    
}


static NSString * BuyerSeacherClassCellID  = @"StarlightListTableViewCellID";

-(UITableView *)zhltableView{
    
    if (!_zhltableView) {
        
        UITableView * zhltableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
        zhltableView.delegate = self;
        zhltableView.dataSource = self;
        zhltableView.backgroundColor = [UIColor whiteColor];
        zhltableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        zhltableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        zhltableView.sectionHeaderHeight = 0;
        zhltableView.showsVerticalScrollIndicator = NO;
        zhltableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:zhltableView];
        
        [zhltableView registerClass:[BuyerSeacherClassCell class] forCellReuseIdentifier:BuyerSeacherClassCellID];
        _zhltableView = zhltableView;
    }
    
    
    return _zhltableView;
}

#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return self.dataArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 40;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    
    
    return CGFLOAT_MIN;
}

#pragma mark  cell处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BuyerSeacherClassCell * cell = [tableView dequeueReusableCellWithIdentifier:BuyerSeacherClassCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary * dic = self.dataArray[indexPath.row];
    cell.titlelab.frame = CGRectMake(10, 0, kScreenWidth - 70, cell.frameHeight);
    cell.titlelab.text = [dic valueForJSONStrKey:@"title"];
    cell.icon.frame = CGRectMake(kScreenWidth - 10 - 18, (cell.frameHeight - 18)/2, 18, 18);
    cell.icon.image = [UIImage imageNamed:@"xkf_xiala2-1"];
    
    if (self.index == indexPath.row) {
        
        cell.icon.hidden = NO;
        
        
    }else{
        
        
        cell.icon.hidden = YES;
    }
    
    
    
    
    
    
    return cell;
    
}
#pragma mark  选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"%@",indexPath);
    
    self.index = indexPath.row;
    [tableView reloadData];
}
-(void)backButtonClicked:(id)sender{
    
    if (self.bolck) {
        self.bolck(self.dataArray[self.index],self.index);
    }
    [super backButtonClicked:sender];
}
@end
